package org.cloudplatform.common.test.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.CuratorCache;
import org.apache.curator.framework.recipes.cache.CuratorCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * 类描述：
 * 作者：徐卫超 (cc)
 * 时间 2022/5/17 17:31
 */
public class NodeCacheDemo {

    CuratorFramework curator;

    private static final String SERVER_PROXY = "192.168.101.22:2181";
    private static final int CONNECTION_TIMEOUT_MS = 3000;
    private static final int SESSION_TIMEOUT_MS = 1000;
    private static final String NAMESPACE = "MyNamespace";

    @Before
    public void init() {
        System.setProperty("zookeeper.sasl.client", "false");
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        // 创建CuratorFramework实例
        this.curator = CuratorFrameworkFactory.builder()
                .connectString(SERVER_PROXY)
                .retryPolicy(retryPolicy)
                .connectionTimeoutMs(CONNECTION_TIMEOUT_MS)
                .sessionTimeoutMs(SESSION_TIMEOUT_MS)
                .namespace(NAMESPACE)
                .build();

        this.curator.start();
    }

    @Test
    public void listenable() throws Exception {



        CuratorCache curatorCache = CuratorCache.build(curator, "/nodeCache"); // nodeCache 必须存在不然会报错
        curatorCache.listenable().addListener(new CuratorCacheListener() {
            @Override
            public void event(Type type, ChildData oldData, ChildData data) {
                if (type.name().equals("NODE_CREATED")) {  //节点创建
                    System.out.printf("CREATED: \t path %s \n", data.getPath());
                } else if (type.name().equals("NODE_CHANGED")) { // 节点修改
                    System.out.printf("CHANGED: \t path %s \n", data.getPath());
                } else { //节点移除
                    System.out.printf("DELETED: \t path %s \n", oldData.getPath());
                }
            }
        });
        curatorCache.start();
        TimeUnit.SECONDS.sleep(5);

        for (int i = 0; i < 5; i++) {
            curator.create().withMode(CreateMode.PERSISTENT).forPath("/nodeCache/" + i, String.valueOf(i).getBytes());
            if (i == 1) {
                for (int j = 0; j < 5; j++) {
                    curator.create().withMode(CreateMode.PERSISTENT).forPath("/nodeCache/1/" + j,
                            String.valueOf(j).getBytes());
                }
                curator.delete().deletingChildrenIfNeeded().forPath("/nodeCache/" + i);
            } else {
                curator.setData().forPath("/nodeCache/" + i, ("修改" + i).getBytes());
                curator.delete().forPath("/nodeCache/" + i);
            }

        }


    }
}
